<?php
defined('IN_TS') or die('Access Denied.');

$userid = aac('user')->isLogin();

//判断发布者状态
if(aac('user')->isPublisher()==false) tsNotice('不好意思，你还没有权限发布内容！');

switch($ts){

	case "":
		$arrOptions = $new['event']->findAll('event_options');
		foreach($arrOptions as $item){
			$strOption[$item['optionname']] = stripslashes($item['optionvalue']);
		}

		if ($strOption['allowpost'] == 0 && $TS_USER['isadmin'] == 0) {
//        if ($TS_APP['allowpost'] == 0 && $TS_USER['isadmin'] == 0) {
            tsNotice('系统设置不允许会员发活动！');
        }

		$title = '发布活动';
		include template("add");
	
		break;
	
	case "do":

		$title = trim($_POST['title']);
		$typeid = intval($_POST['typeid']);
		$starttime = trim($_POST['starttime']);
		$endtime = trim($_POST['endtime']);
		$limittime = trim($_POST['limittime']);
		$address = trim($_POST['address']);
		$coordinate = trim($_POST['coordinate']);  //坐标
		$content = tsClean($_POST['content']);

        $price = floatval(trim($_POST['price']));
		$quota = intval(trim($_POST['quota']));
		$duty = intval(trim($_POST['duty']));

		$starttime = trim(str_replace('AM','',$starttime));
        $starttime = trim(str_replace('PM','',$starttime));
		$starttime = strtotime($starttime);

        $endtime = trim(str_replace('AM','',$endtime));
        $endtime = trim(str_replace('PM','',$endtime));
		$endtime = strtotime($endtime);

		$limittime = trim(str_replace('AM','',$limittime));
		$limittime = trim(str_replace('PM','',$limittime));
		$limittime = strtotime($limittime);

		$photo = trim($_POST['photo']);

		if($title == '' || $content == ''){
			tsNotice('标题和内容不能为空');
		}

        //1审核后显示0不审核
        if ($TS_APP['isaudit'] == 1) {
            $isaudit = 1;
        } else {
            $isaudit = 0;
        }

		if($photo==''){
			tsNotice('必须上传封面照片！');
		};
		$img_path = $photo;

		// $img_path = getTextPhotos(tsDecode($content));
		// $count = count($img_path);
		// $img_path = $img_path[$count-1];
		// if($img_path==''){
		// 	tsNotice('请至少插入一张图片作为封面！');
		// };
		//$url=preg_replace('#/$#','',SITE_URL);
		//$img_path = $url.$img_path;



		$eventid = $new['event']->create('event',array(

			'userid'	=> $userid,
			'title'	=> $title,
			'typeid' => $typeid,
			'starttime'	=> $starttime,
			'limittime' => $limittime,
			'endtime'	=> $endtime,
			'address'	=> $address,
			'coordinate'	=> $coordinate,
			'content' => $content,
            'price'=>$price,
			'isaudit'=>$isaudit,
			'addtime'	=> time(),
			'quota'=>$quota,
			'duty'=>$duty,
			'path'=> dirname($img_path),
			'photo'=> $img_path,
		));



		//上传
		// $arrUpload = tsUploadPhotoUrl($img_path,$eventid,'event',array('jpg','gif','png'));
		// if($arrUpload){
		// 	$new['event']->update('event',array(
		// 		'eventid'=>$eventid,
		// 	),array(
		// 		'path'=>$arrUpload['path'],
		// 		'photo'=>$arrUpload['url'],
		// 	));
		// }
		// 对积分进行处理
		aac('user') -> doScore($TS_URL['app'], $TS_URL['ac'], $TS_URL['ts']);

		header("Location: ".tsUrl('event','show',array('id'=>$eventid)));

		break;

	//地图 
	case "map":
	
		$dd = isset($_GET['dd']) ? $_GET['dd'] : '中国北京';

		include template('add_map');
	
		break;

}